package com.woniuxy.controller;

import java.math.BigDecimal;
import java.security.Principal;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.woniuxy.dto.PageDto;
import com.woniuxy.entity.CategoryOne;
import com.woniuxy.entity.CategoryTwo;
import com.woniuxy.entity.HeatBidding;
import com.woniuxy.entity.Product;
import com.woniuxy.entity.User;
import com.woniuxy.service.BiddingService;
import com.woniuxy.service.LogService;
import com.woniuxy.service.ProductService;
import com.woniuxy.service.UserService;
import com.woniuxy.utils.ResultUtil;
import java.util.Arrays;

import org.apache.ibatis.annotations.Param;

@RestController
@RequestMapping("/product")
public class ProductController {

	@Autowired
	UserService userService;
	@Autowired
	LogService logservice;
	@Autowired
	private ProductService productService;

	@Autowired
	private BiddingService biddingService;

	@RequestMapping("/goodesByUid")
	public PageDto<Product> goodesByUid(HttpServletRequest request) {
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user = (User) session.getAttribute("user");

		// 拿到传递过来的值
		int page = Integer.parseInt(request.getParameter("page"));
		System.out.println("page是" + page);
		// 获取卖家uid
		// 调用service

		System.out.println("都有哪些参数" + productService.goodesByUid(user.getU_id(), page));

		return productService.goodesByUid(user.getU_id(), page);

	}

	// 上架商品
	@RequestMapping("/getOneCategory")
	public List<CategoryOne> oneMenu() {
		List<CategoryOne> categoryOne = productService.findoneMenu();
		System.out.println(categoryOne);
		return categoryOne;

	}

	@RequestMapping("/getTwoCategory")
	public List<CategoryTwo> getTwo() {
		List<CategoryTwo> categoryTwo = productService.findTwoMenu();
		System.out.println(categoryTwo);
		return categoryTwo;

	}

	@RequestMapping("/uploadProduct")
	public ResultUtil<Object> addProduct(Product product, HttpServletRequest request) {

		System.out.println("前端传递过来的值" + product);
		// 添加日志信息

		logservice.insertgoodsInFo(product);

		// 新建结果对象，给网页返回判断的结果
		ResultUtil<Object> resultUtil = new ResultUtil<>();
		// System.out.println("======================>"+product);
		// System.out.println(product.getPicture());
		// 获得当前账户名
		Principal principal = request.getUserPrincipal();
		User user = new User().setAccount(principal.getName());
		User user2 = userService.findByAccount(user.getAccount());

		Date auctionTime = new Date(new Date().getTime() + (1000 * 60 * 60 * 24));

		// System.out.println("============="+user2.getU_id());
		product.setU_id(user2.getU_id()).setAudit_status(0).setDate_issued(new Date()).setAuction_time(auctionTime);
		boolean result = productService.addProduct(product);
		if (!result) {
			resultUtil.setStatus(ResultUtil.ERROR);
			// 添加日志小心
			logservice.uploadProduct(product);

			return resultUtil;
		}
		return resultUtil.setStatus(ResultUtil.SUCCESS);
	}

	@RequestMapping("/findProductById")
	public ResultUtil<Product> findProductById(int p_id) {// 查询商品详情
		int u_id = 0;
		// System.out.println("00000000000000000000000000");
		Session session = SecurityUtils.getSubject().getSession();
		Object attribute = session.getAttribute("u_id");
		if (attribute != null) {
			u_id = (int) attribute;
		}
		// .out.println("1111111111111111111111");
		Product goods = productService.findProductById(p_id, u_id);

		ResultUtil<Product> resultUtil = new ResultUtil<>();
		resultUtil.setData(goods);
		resultUtil.setStatus(ResultUtil.SUCCESS);
		System.out.println(resultUtil.getData());
		return resultUtil;
	}

	@RequestMapping("/findHeatBidding")
	public ResultUtil<List<HeatBidding>> findHeatBidding() {
		List<HeatBidding> findHeatBidding = productService.findHeatBidding();
		ResultUtil<List<HeatBidding>> resultUtil = new ResultUtil<>();
		resultUtil.setData(findHeatBidding);
		resultUtil.setStatus(ResultUtil.SUCCESS);
		System.out.println(resultUtil.getData());
		return resultUtil;

	}

	@RequestMapping("/currentPrice")
	public ResultUtil<BigDecimal> currentPrice(int p_id) {// 查询商品竞拍当前价格
		System.err.println("current");
		BigDecimal currentPrice = biddingService.currentPrice(p_id);
		ResultUtil<BigDecimal> resultUtil = new ResultUtil<>();
		resultUtil.setData(currentPrice);
		resultUtil.setStatus(ResultUtil.SUCCESS);
		System.out.println(resultUtil.getData());
		return resultUtil;

	}

	@RequestMapping("/findCategory")
	public List<CategoryTwo> findByCategory(int co_id) {
		return productService.findCategory(co_id);
	}

	@RequestMapping("/findByCategory")
	public PageDto<Product> findByCategory(int co_id, int pageNum) {
		return productService.findByCategory(co_id, pageNum);
	}

	@RequestMapping("/findByCt_id")
	public PageDto<Product> findByCt_id(int co_id, int ct_id, int pageNum) {
		return productService.findByCt_id(co_id, ct_id, pageNum);
	}

	@RequestMapping("/findByName")
	public PageDto<Product> findByName(String name, int pageNum) {
		return productService.findByName(name, pageNum);
	}

	@RequestMapping("/findByStatus")
	public PageDto<Product> findByStatus(int audit_status, int pageNum) {
		return productService.findByStatus(audit_status, pageNum);
	}

	@RequestMapping("/findByStatusAndName")
	public PageDto<Product> findByStatusAndName(String name, int audit_status, int pageNum) {
		return productService.findByStatusAndName(name, audit_status, pageNum);
	}

	@RequestMapping("/findHeat")
	public List<Product> findHeat() {
		return productService.findHeat();
	}

	@RequestMapping("/findHeat2")
	public List<Product> findHeat2() {
		return productService.findHeat2();
	}

	@RequestMapping("/findByLike")
	public PageDto<Product> findByLike(String name, int pageNum) {
		return productService.findByLike(name, pageNum);
	}

	@RequestMapping("/findByNewest")
	public PageDto<Product> findByNewest(int co_id, int pageNum) {
		return productService.findByNewest(co_id, pageNum);
	}

	@RequestMapping("/findByHotest")
	public PageDto<Product> findByHotest(int co_id, int pageNum) {
		return productService.findByHotest(co_id, pageNum);
	}

	@RequestMapping("/findByChargeNum")
	public PageDto<Product> findByChargeNum(int co_id, int pageNum) {
		return productService.findByChargeNum(co_id, pageNum);
	}

	@RequestMapping("/findByPriceUpAndDown")
	public PageDto<Product> findByPriceUpAndDown(int co_id, boolean flag, int pageNum) {
		return productService.findByPriceUpAndDown(co_id, flag, pageNum);
	}

	@RequestMapping("/updateHeat")
	public int updateHeat(int p_id) {
		return productService.updateHeat(p_id);
	}

	@RequestMapping("/isCollected")
	// 查询是否收藏过该商品
	public ResultUtil<Object> isCollected(int p_id) {
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user = (User) session.getAttribute("user");
		 System.out.println(user.getU_id()+"user");
		Boolean collected = productService.isCollected(p_id, user.getU_id());
		ResultUtil<Object> resultUtil = new ResultUtil<>();
		if (collected) {
			resultUtil.setStatus(ResultUtil.SUCCESS);
			resultUtil.setMessage(ResultUtil.SUCCESS);
			System.err.println("已收藏过该商品");
			return resultUtil;
		}
		resultUtil.setStatus(ResultUtil.ERROR);
		resultUtil.setMessage(ResultUtil.ERROR);
		System.err.println("未收藏过该商品");
		return resultUtil;

	}

	@RequestMapping("/isSigned") // 是否预约过该商品
	public ResultUtil<Object> isSigineD(int p_id, int u_id, int br_id) {

		boolean signed = productService.isSigned(p_id, u_id);
		ResultUtil<Object> resultUtil = new ResultUtil<>();
		if (signed) {
			resultUtil.setStatus(ResultUtil.SUCCESS);
			resultUtil.setMessage(ResultUtil.SUCCESS);
			System.err.println("已预约过该商品");
			return resultUtil;
		}
		resultUtil.setStatus(ResultUtil.ERROR);
		resultUtil.setMessage(ResultUtil.ERROR);
		System.err.println("未预约过该商品");
		return resultUtil;

	}

	@RequestMapping("/allProducts")
	public ResultUtil<Object> allProducts(int pageNum, int pageSize) {
		ResultUtil<Object> resultUtil = new ResultUtil<>();
		resultUtil.setData(productService.allProducts(pageNum, pageSize));
		resultUtil.setStatus(ResultUtil.SUCCESS);
		return resultUtil;

	}

	@RequestMapping("/batchUpdateProducts")
	public ResultUtil<Object> batchUpdateProducts(int audit_status, String ids) {

		// 转换成int[]
		String[] idStrings = ids.split(",");
		int[] idsArray = new int[idStrings.length];
		for (int j = 0; j < idStrings.length; j++) {
			idsArray[j] = Integer.parseInt(idStrings[j]);
		}
		// System.out.println(Arrays.toString(idsArray));

		ResultUtil<Object> resultUtil = new ResultUtil<>();
		int result = productService.batchUpdateProducts(audit_status, idsArray);
		resultUtil.setData(result);
		if (result > 0) {
			resultUtil.setStatus(ResultUtil.SUCCESS);
		} else {
			resultUtil.setStatus(ResultUtil.ERROR);
		}
		return resultUtil;
	}

	@RequestMapping("/batchDeleteProducts")
	public ResultUtil<Object> batchDeleteProducts(String ids) {
		// 转换成int[]
		String[] idStrings = ids.split(",");
		int[] idsArray = new int[idStrings.length];
		for (int j = 0; j < idStrings.length; j++) {
			idsArray[j] = Integer.parseInt(idStrings[j]);
		}
		// System.out.println(Arrays.toString(idsArray));

		ResultUtil<Object> resultUtil = new ResultUtil<>();
		int result = productService.batchDeleteProducts(idsArray);
		resultUtil.setData(result);
		if (result > 0) {
			resultUtil.setStatus(ResultUtil.SUCCESS);
		} else {
			resultUtil.setStatus(ResultUtil.ERROR);
		}

		return resultUtil;
	}

}
